A r t i c l e s
Navigation

Note: This site is
a bit older, personal views
may have changed.

M a i n P a g e

D i r e c t o r y

Var versus Out Parameters


I'm patronizing many people that read this wiki but I want to make clear the difference between OUT parameters and VAR parameters. OUT parameters are more restricted and aren't used as often simply because not many people know about it.

OUT parameters do not take note of the incoming value that you passed in. Out parameters ignore the variable's value that was set previous to the function call.

After the function is executed, an OUT parameter acts similar to a VAR parameter in that an out parameter can be used to return a value out of the function.

VAR parameters differ in that they do not ignore the value you have set the variable to previous to the function call.

The program below explains the difference. Run the program to find out.

program test; {$mode objfpc} {$H+}

procedure test1(out s: string);
begin
  s:= s + 'blah';
end;

procedure test2(var s: string);
begin
  s:= s + 'blah';
end;

var
  s: string;
begin
  s:= 'zzzz';
  test1(s);
  writeln(s);

  s:= 'zzzz';
  test2(s);
  writeln(s);

  readln;
end.
If you were making a function like this:
  inc(var x: integer);
you could not use a "OUT" parameter because INC() relies on the previous value of x.

About
This site is about programming and other things.
_ _ _